<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * Smarty Configuration Class
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 21.04.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * Class mySmarty
    ****
    * This class configures the Smarty template parser by
    * setting all the neccessary paths and overriding some
    * default settings.
    ****
    * @package Smarty
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 21.04.2005
    * @copyright essentialmind.com 2005
    *
    * @link http://smarty.php.net
    *
    * @2do outsource all settings to the ini file or the
    *      database
    ***/
    class mySmarty extends Smarty{

        /***
        ------------- START VARIABLES -------------
        |
         Note: you DON'T edit these, capiche?

        ***/

        /**
        * Whether the processor was called on the admin side or not
        *
        * @var boolean
        */
        public $admin = false;

        /**
         * All the available language data
         *
         * @var array
         */
        public $lang = array();

        /**
         * name of the language file to use
         *
         * @var unknown
         */
        private $langFile;

        /**
         * reference to the language file parser
         *
         * @var unknown
         */
        private $langParser;

        /***
        |
        -------------  END VARIABLES  -------------
        ***/


        /**
        * Constructor
        *
        * @param boolean $admin - true in case work is done on the admin side
        * @return void
        **/
        public function __construct($admin=false){
            //mark admin section or not
            $this->admin = $admin;

            // -- / Start Settings / --

            // -- Control Templates
            // check whether new templates need recompilation
            $this->compile_check    = $GLOBALS['_CONFIG']['ccheck'];

            // -- Various paths
            // if the call is placed from the client, then load the
            // client template set, otherwise load the admin templates
            if(!$this->admin){
                if($GLOBALS['_CONFIG']['tset']){
                    $this->template_dir = array(ROOT.'templates/'.$GLOBALS['_CONFIG']['tset'],INA_ROOT.'templates/common');
                }else{
                    $this->template_dir = array(ROOT.'templates/main',INA_ROOT.'templates/common');
                }

                $this->compile_dir	= ROOT.'cache'.DIRECTORY_SEPARATOR.'client_temp_c';
                $this->config_dir   = ROOT.'templates'.DIRECTORY_SEPARATOR.'main'.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$GLOBALS['_CONFIG']['lang_cl'];


                //client pages may be cached
                $this->caching 	        = $GLOBALS['_CONFIG']['cache'];

                //lifetime of cache in seconds
                $this->cache_lifetime   = $GLOBALS['_CONFIG']['clife'];

                //send a modified header?
                $this->cache_modified_check = $GLOBALS['_CONFIG']['cmodc'];

                //set the cache firectory
                $this->cache_dir        = ROOT.'cache'.DIRECTORY_SEPARATOR.'content';

                //now some language magic
                //create config file reader
	            $this->langParser       = new Config_File(ROOT.'templates'.DIRECTORY_SEPARATOR.'main'.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$GLOBALS['_CONFIG']['lang_cl']);


                //add custom plugins management
                if(is_dir(ROOT.'smartyPlugins')){
                    $this->plugins_dir[] = ROOT.'smartyPlugins';
                }

            }else{
                //admin templates are loaded centrally for the whole system
                //yet the compiled templates are stored locally
                $this->template_dir = array(ROOT.'templates/admin',INA_ROOT.'templates/admin');
                $this->compile_dir	= ROOT.'cache'.DIRECTORY_SEPARATOR.'admin_temp_c';
                $this->config_dir   = INA_ROOT.'templates'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$GLOBALS['_CONFIG']['lang_ad'];

                //admin pages are never cached
                $this->caching 	    = false;

                //now some language magic
                //create config file reader
	            $this->langParser   = new Config_File(INA_ROOT.'templates'.DIRECTORY_SEPARATOR.'admin'.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$GLOBALS['_CONFIG']['lang_ad']);
            }

            //print_r($this->template_dir);
            //this is to be changed, we do not want to rely on this
            //outsource all error messages to a separate file and all
            //system data to a separate file
            $this->setLangFile('main.txt');

            // -- Other Settings
            // enable the use of subdirs?
            $this->use_sub_dirs     = $GLOBALS['_CONFIG']['subdirs'];

            // debug console?
            $this->debugging        = $GLOBALS['_CONFIG']['debug'];

            // plugins dir?
            $this->plugins_dir[]    = INA_ROOT.'smartyPlugins';

            // -- / Finish Settings / --

            // -- / Start Init Smarty / --
            // init smarty
            $this->Smarty();

            //register custom blocks
            $this->register_block("dynamic", "smarty_block_dynamic", false);

            //register modifiers
            $this->register_modifier("sslash", "stripslashes");
            // -- / End Init Smarty / --
        }


        /**
        * Overloader of smarty's display such that some preset variables
        * are passed to smarty. Moreover error reporting is lowered by one
        * level for the display of the templates such that no notices are
        * raised in case template variables are not defined
        *
        * @param string $template
        * @param string $cacheid
        * @return
        **/
        public function display($template,$cacheid = ""){
            //global assigns
            $this->assignDefaults();

            //minimize whitespace
            //$this->autoload_filters = array('output' => array('trimwhitespace'));

            //don't show notices
            error_reporting(E_ALL ^ E_NOTICE);

            //fetch template
            parent::display($template,$cacheid);

            //reset error reporting level
            error_reporting(E_ALL);
        }


        /**
        * Same as above, but for fetch
        *
        * @param string $template
        * @param string $cacheid
        * @return
        **/
        public function preFetch($template,$cacheid = ""){
            //global assigns
            $this->assignDefaults();

            //don't show notices
            error_reporting(E_ALL ^ E_NOTICE);

            //fetch template
            return parent::fetch($template,$cacheid);

            //reset error reporting level
            error_reporting(E_ALL);
        }



        /**
         * trigger Smarty error, now PHP5 compliant
         *
         * @param string $error_msg
         * @param integer $error_type
         */
        function trigger_error($error_msg, $error_type = E_USER_WARNING)
        {
            //trigger_error("Smarty error: $error_msg", $error_type);
            throw new exception("Smarty error: $error_msg");
        }



        /**
         * Loads language settings for a specific section of the lang file
         *
         * @param string $section
         * @return void
         */
        public function loadLangData($section){
            $this->lang = array_merge($this->lang,$this->langParser->get($this->getLangFile(),$section));
        }

        /**
         * Loads language settings from any file, section does not have to be specified
         * the above is to be deprecated asap
         *
         * @param string $file
         * @param string $section
         */
        public function loadLang($file,$section=null){
            $this->lang = array_merge($this->lang,$this->langParser->get($file,$section));
        }


        /**
        * Sets a number of default template variables that
        * may be handy when creating templates
        *
        * @return void
        **/
        private function assignDefaults(){
            // default INA variables placeholder
            $INA = array();

            // INA Version

            $INA['version'] = '0.85 build 21.06.2011 (betty)';


            // INA Paths
            $INA['root']            = $GLOBALS['_CONFIG']['server_root'];
            $INA['files']	        = $GLOBALS['_CONFIG']['file_root'];
            $INA['maps']            = $GLOBALS['_CONFIG']['maps_key'];
            $INA['analytics']	    = $GLOBALS['_CONFIG']['analytics'];
            $INA['admin']           = $GLOBALS['_CONFIG']['admin_root'];
            $INA['title']           = $GLOBALS['_CONFIG']['title'];
            $INA['subtitle']        = $GLOBALS['_CONFIG']['subtitle'];
            $INA['vstamp']          = $GLOBALS['_CONFIG']['version'];
            
            // =======================
            // = thumbnails settings =
            // =======================
            
            // number of thumbsnails
            // $INA['thumb_nums']      = $GLOBALS['_CONFIG']['thumb_nums'];
            //             setcookie('thumb_nums', $GLOBALS['_CONFIG']['thumb_nums'], time()+3600);         
            //             $thumbNums = (int)$GLOBALS['_CONFIG']['thumb_nums'];
            //             
            //             if ($thumbNums > 0) {
            //                 for ($i = 1; $i <= $thumbNums; $i++) {
            //                     $INA['thumb_use_'.$i]         =   $GLOBALS['_CONFIG']['thumb_use_'.$i];
            //                     $INA['thumb_square_'.$i]      =   $GLOBALS['_CONFIG']['thumb_square_'.$i];
            //                     $INA['thumb_max_height_'.$i]  =   $GLOBALS['_CONFIG']['thumb_max_height_'.$i];
            //                     $INA['thumb_max_width_'.$i]   =   $GLOBALS['_CONFIG']['thumb_max_width_'.$i];
            //                     
            //                     // push thumbnails settings to cookies
            //                     setcookie('thumb_use_'.$i,        $GLOBALS['_CONFIG']['thumb_use_'.$i], time()+3600);
            //                     setcookie('thumb_square_'.$i,     $GLOBALS['_CONFIG']['thumb_suare_'.$i], time()+3600);
            //                     setcookie('thumb_max_height_'.$i, $GLOBALS['_CONFIG']['thumb_max_height_'.$i], time()+3600);
            //                     setcookie('thumb_max_width_'.$i,  $GLOBALS['_CONFIG']['thumb_max_width_'.$i], time()+3600);
            //                 }
            //             }
            $INA['enable_order_position'] = $GLOBALS['_CONFIG']['enable_order_position'];
            $INA['adminTemplate']   = $INA['root']  .'templates/admin/';
            if($GLOBALS['_CONFIG']['tset']){
                $INA['clientTemplate']  = $INA['root']  .'templates/'.$GLOBALS['_CONFIG']['tset'].'/';
            }else{
                $INA['clientTemplate']  = $INA['root']  .'templates/main/';
            }

            $INA['templates']       = $INA['root']  .'templates/';
            $INA['page']            = (isset($_GET['page'])) ? $_GET['page'].'.html' : null;

            // this bit is important, defines the current page either in the client
            // or in the admin environments
            if($this->admin){ //admin side
                $INA['this']    = $INA['admin'];

                //check if there is a subsection call
                if(!empty($_GET['view'])){
                    $INA['this']    = $INA['this'] .$_GET['view']  .'/';
                }

            }else{ //client side
                $INA['this']        = $INA['root'];

                //check if there is a subpage call
                if(!empty($_GET['data'])){
                    $INA['this']    = $INA['this'].$_GET['data'].'/';
                }

                $INA['folder']      = $INA['this'];
                
                if(!empty($_GET['page'])){
                    $INA['this']    = $INA['this'].$INA['page'];
                }
            }

            //set client side lang file
            $INA['lang']            = 'main.txt';   //this needs changing, we do not want to rely on this

            // System
            $INA['qs']              = $_SERVER['QUERY_STRING'];

            // See if we can fish out the memory usage
            if(function_exists('memory_get_usage')){
                $INA['memory']      = round(memory_get_usage()/(1024*1024),2);
            }

            // pass the data to smarty
            $this->assign("INA",$INA);
        }


        /**
         * Sets an internal lang file name to use
         *
         * @param string
         * @return void
         */
        private function setLangFile($file){
            $this->langFile = $file;
        }


        /**
         * Returns the internal lang file name
         *
         * @return string
         */
        private function getLangFile(){
            return $this->langFile;
        }
    }


    /**
    * Smarty block function, that is used for non-cacheable blocks
    *
    * @param string $param
    * @param string $content
    * @param mySmarty $smarty
    * @return
    **/
    function smarty_block_dynamic($param, $content, $smarty) {
        return $content;
    }
?>
